package com.jege.jpa.many2many;

import com.jege.jpa.many2many.convert.EnumConverter;
import com.jege.jpa.many2many.enums.OpenState;

import javax.persistence.*;
import java.util.List;

/**
 *
 * Created by ange on 2017/1/28.
 */
@Entity
public class Resource {
    private int id;
    private String name;
    private String url;
    private OpenState status;
    private List<Role> roles;
    private ResourceMoudle moudle;

    @Id
    @Column(name = "id", nullable = false)
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Basic
    @Column(name = "name", nullable = false, length = 40)
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Basic
    @Column(name = "url", nullable = false, length = 100)
    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }


    @Basic
    @Column(name = "status", nullable = false)
    @Convert(converter = EnumConverter.class)
    public OpenState getStatus() {
        return status;
    }

    public void setStatus(OpenState status) {
        this.status = status;
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Resource resource = (Resource) o;

        if (id != resource.id) return false;
        if (name != null ? !name.equals(resource.name) : resource.name != null) return false;
        if (url != null ? !url.equals(resource.url) : resource.url != null) return false;
        if (status != null ? !status.equals(resource.status) : resource.status != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = id;
        result = 31 * result + (name != null ? name.hashCode() : 0);
        result = 31 * result + (url != null ? url.hashCode() : 0);
        result = 31 * result + (status != null ? status.hashCode() : 0);
        return result;
    }

    @ManyToMany(mappedBy = "resources")
    public List<Role> getRoles() {
        return roles;
    }

    public void setRoles(List<Role> roles) {
        this.roles = roles;
    }

    @ManyToOne
    @JoinColumn(name = "moudle_id", referencedColumnName = "id")
    public ResourceMoudle getMoudle() {
        return moudle;
    }

    public void setMoudle(ResourceMoudle moudle) {
        this.moudle = moudle;
    }
}
